<template>
  <div>
    <Header>
      <van-icon name="arrow-left" size="22" slot="left" />
      <span slot="center">编辑地址</span>
    </Header>
    <van-address-edit :area-list="areaList" show-postal show-set-default :address-info="addressInfo"
      :area-columns-placeholder="['请选择', '请选择', '请选择']" @save="onSave" />
  </div>
</template>

<script>
import { areaList } from '@vant/area-data'
import Header from '@/components/common/Header.vue'
import { get_address, update_address } from '@/network/api/address'
export default {
  name: 'edit',
  components: {
    Header
  },
  data() {
    return {
      id: 0, // 表user-address的id
      areaList,
      addressInfo: {},
    }
  },
  methods: {
    async onSave(content) {
      content.isDefault_int = content.isDefault ? 1 : 0
      await update_address(content)
      this.$router.back()
    },
    parseAddress(address) {
      address.isDefault = address.isDefault_int === 1 ? true : false
      return address
    }
  },
  async created() {
    this.id = this.$route.query.id
    const res = await get_address(this.id)
    this.addressInfo = this.parseAddress(res.data[0])
  }
}
</script>

<style scoped>

</style>

